package com.zhoug.common.view.edit;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-07-15
 * @Description:
 */
public class ZMUIEditText extends androidx.appcompat.widget.AppCompatEditText {
    private List<ZMUITextWatcherWrap> mListeners;

    public ZMUIEditText(@NonNull Context context) {
        super(context);
    }

    public ZMUIEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ZMUIEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addTextChangedListener(TextWatcher watcher) {
        super.addTextChangedListener(watcher);
    }



    public void removeTextChangedListener(ZMUITextWatcher watcher) {
        ZMUITextWatcherWrap textWatcherWrap = findTextWatcherWrap(watcher);
        if(textWatcherWrap!=null){
            super.removeTextChangedListener(textWatcherWrap);
            mListeners.remove(textWatcherWrap);
        }
    }

    public void addTextChangedListener(ZMUITextWatcher watcher) {
        if (mListeners == null) {
            mListeners = new ArrayList<>();
        }
        ZMUITextWatcherWrap textWatcherWrap = findTextWatcherWrap(watcher);
        if (textWatcherWrap == null) {
            ZMUITextWatcherWrap zmuiTextWatcherWrap = new ZMUITextWatcherWrap(watcher);
            super.addTextChangedListener(zmuiTextWatcherWrap);
            mListeners.add(zmuiTextWatcherWrap);
        }
    }

    private ZMUITextWatcherWrap findTextWatcherWrap(ZMUITextWatcher watcher) {
        if (mListeners != null && !mListeners.isEmpty()) {
            for (int i = 0; i < mListeners.size(); i++) {
                ZMUITextWatcherWrap zmuiTextWatcherWrap = mListeners.get(i);
                if (zmuiTextWatcherWrap.mListener == watcher) {
                    return zmuiTextWatcherWrap;
                }
            }
        }
        return null;
    }


    private class ZMUITextWatcherWrap implements TextWatcher {
        private final ZMUITextWatcher mListener;

        public ZMUITextWatcherWrap(ZMUITextWatcher listener) {
            this.mListener = listener;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            mListener.beforeTextChanged(self(), s, start, count, after);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mListener.onTextChanged(self(), s, start, before, count);

        }

        @Override
        public void afterTextChanged(Editable s) {
            mListener.afterTextChanged(self(), s);

        }
    }

    private ZMUIEditText self() {
        return this;
    }

}
